package tianrun.ziguan.api.calculate.config.utils;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collector;
import java.util.stream.Collectors;

public class CollectionUtil {
    private CollectionUtil() {
    }

    public static <E> boolean isEmpty(Collection<E> c) {
        return c == null || c.isEmpty() || c.stream().noneMatch(Objects::nonNull);
    }

    public static <K, V> boolean isEmpty(Map<K, V> map) {
        return map == null || map.isEmpty();
    }

    public static <K, V> boolean isNotEmpty(Map<K, V> map) {
        return !isEmpty(map);
    }

    public static <E> boolean isNotEmpty(Collection<E> c) {
        return !isEmpty(c);
    }

    public static <T> boolean isEmpty(T[] t) {
        return t == null || t.length == 0;
    }

    public static <T> boolean isNotEmpty(T[] t) {
        return !isEmpty(t);
    }

    public static <E, F> boolean isEqualsSize(List<E> source, List<F> target) {
        if (source == null || target == null) {
            return false;
        }
        return source.size() == target.size();
    }

    public static <E> Long size(Collection<E> c) {
        return isEmpty(c) ? 0L : c.size();
    }

    public static <T> List<T> nonNullList(List<T> list) {
        return list == null ? Collections.emptyList() : list;
    }

    public static <T> Collection<T> nonNullList(Collection<T> list) {
        return list == null ? Collections.emptyList() : list;
    }

    //收集器
    public static <T> Collector<T, ?, List<T>> toSortedList(Comparator<? super T> c) {
        return Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(c)), ArrayList::new);
    }

    public static <T, K> Map<K, List<T>> groupingBy(Collection<T> collection, Function<? super T, ? extends K> classifier) {
        if (collection == null || collection.isEmpty()) {
            return Collections.emptyMap();
        }
        return collection.stream().collect(Collectors.groupingBy(classifier));
    }

    public static <T, K, V> Map<K, List<V>> groupingBy(Collection<T> collection, Function<? super T, ? extends K> classifier, Function<? super T, ? extends V> classifier1) {
        if (collection == null || collection.isEmpty()) {
            return Collections.emptyMap();
        }
        return collection.stream().collect(Collectors.groupingBy(classifier, Collectors.mapping(classifier1, Collectors.toList())));
    }

    public static <T, K> Map<K, T> toMap(Collection<T> collection, Function<? super T, ? extends K> classifier) {
        if (collection == null || collection.isEmpty()) {
            return Collections.emptyMap();
        }
        return collection.stream().collect(Collectors.toMap(classifier, java.util.function.Function.identity(), (a, b) -> a));
    }

    public static <T, K, R> Map<K, R> toMap(Collection<T> collection, Function<? super T, ? extends K> classifier, Function<? super T, ? extends R> classifier1) {
        if (collection == null || collection.isEmpty()) {
            return Collections.emptyMap();
        }
        return collection.stream().collect(Collectors.toMap(classifier, classifier1, (a, b) -> a));
    }

    public static <T, K> List<K> toList(Collection<T> collection, Function<? super T, ? extends K> classifier) {
        if (collection == null || collection.isEmpty()) {
            return Collections.emptyList();
        }
        return collection.stream().map(classifier).collect(Collectors.toList());
    }

    public static <T, K> Set<K> toSet(Collection<T> collection, Function<? super T, ? extends K> classifier) {
        if (collection == null || collection.isEmpty()) {
            return Collections.emptySet();
        }
        return collection.stream().map(classifier).collect(Collectors.toSet());
    }
}
